package unrn.isiii.bean;

import java.util.ArrayList;
import java.util.List;

import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("asistenciaBean")
@Scope("application")
public class AsistenciaBean {

	private static final Logger logger = LoggerFactory
			.getLogger(AsistenciaBean.class);

	private int countLoggin;

	private int countVotaron;

	private int countSi;

	private int countNo;

	public int getCountLoggin() {
		return countLoggin;
	}

	public void setCountLoggin(int countLoggin) {
		this.countLoggin = countLoggin;
	}

	public int getCountVotaron() {
		return countVotaron;
	}

	public void setCountVotaron(int countVotaron) {
		this.countVotaron = countVotaron;
	}

	private List<String> presentes = new ArrayList<String>(0);

	public List<String> getPresentes() {
		return presentes;
	}

	public void setPresentes(List<String> presentes) {
		this.presentes = presentes;
	}

	public int getCountSi() {
		return countSi;
	}

	public void setCountSi(int countSi) {
		this.countSi = countSi;
	}

	public int getCountNo() {
		return countNo;
	}

	public void setCountNo(int countNo) {
		this.countNo = countNo;
	}

	public synchronized void add(String usuario) {
		countLoggin++;
		getPresentes().add(usuario);
	}

	 private final static String CHANNEL = "/{user}/";
	
	public void iniciovotar() {
		EventBus eventBus = EventBusFactory.getDefault().eventBus();

		setCountVotaron(0);
		setCountNo(0);
		setCountSi(0);

		for (String usuario : getPresentes()) {
			eventBus.publish("/" + usuario, String.valueOf(""));
			  eventBus.publish(CHANNEL + "*",usuario);
		}

	}

	public synchronized Boolean votaronTodos() {
		return (getCountLoggin() == getCountVotaron());
	}

	public synchronized void votar() {
		countVotaron++;
	}

	public synchronized void votarSi() {
		countSi++;
	}

	public synchronized void votarNo() {
		countNo++;
	}

}
